package com.haima.bigdata.query.tdd.frame;

import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class RestUtils {
    public static String execute(CloseableHttpClient client, HttpUriRequest req) throws IOException {
        ResponseHandler<String> responseHandler = response -> {
            HttpEntity entity = response.getEntity();
            return entity != null ? EntityUtils.toString(entity) : null;
        };
        return client.execute(req, responseHandler);
    }

    public static String executeBak(CloseableHttpClient httpclient, HttpUriRequest req) throws IOException {
        ResponseHandler<String> responseHandler = response -> {
            int status = response.getStatusLine().getStatusCode();
            if (status >= 200 && status < 300) {
                HttpEntity entity = response.getEntity();
                return entity != null ? EntityUtils.toString(entity) : null;
            } else {
                throw new ClientProtocolException("Unexpected response status: " + status);
            }
        };
        return httpclient.execute(req, responseHandler);
    }

    public static String put(String url, String body) throws IOException {
        return put(url, body, "utf-8");
    }

    public static String put(String url, String body, String encode) throws IOException {
        try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
            HttpPut req = new HttpPut(url);
            req.setEntity(new StringEntity(body, encode));
            return execute(httpclient, req);
        }
    }

    public static String post(String url, String body) throws IOException {
        return post(url, body, "utf-8");
    }

    public static String post(String url, String body, String encode) throws IOException {
        System.out.println(url);
        System.out.println(body);
        try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
            HttpPost req = new HttpPost(url);
            req.setHeader("Content-type", "application/json; charset=" + encode);
            req.setEntity(new StringEntity(body, encode));
            return execute(httpclient, req);
        }
    }


    public static String get(String url) throws IOException {
        try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
            HttpGet req = new HttpGet(url);
            return execute(httpclient, req);
        }
    }

    public static String delete(String url) throws IOException {
        try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
            HttpDelete req = new HttpDelete(url);
            return execute(httpclient, req);
        }
    }


}
